package ${packageName}.api.${moduleName}.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ${packageName}.api.${moduleName}.dao.${moduleNameU}Dao;
import ${packageName}.api.${moduleName}.service.${moduleNameU}Service;
import ${packageName}.api.${moduleName}.service.model.CreateOrUpdateSpModel;
import ${packageName}.api.${moduleName}.service.model.CreateOrUpdateSrModel;
import ${packageName}.api.${moduleName}.service.model.DeleteSpModel;
import ${packageName}.api.${moduleName}.service.model.DeleteSrModel;
import ${packageName}.api.${moduleName}.service.model.GetSpModel;
import ${packageName}.api.${moduleName}.service.model.GetSrModel;
import ${packageName}.api.${moduleName}.service.model.ListSpModel;
import ${packageName}.api.${moduleName}.service.model.ListSrModel;
import ${packageName}.helper.mybatis.dao.${dbTablePrefixU}${dbTableJavaNameU}Mapper;
import ${packageName}.helper.mybatis.model.${dbTablePrefixU}${dbTableJavaNameU};
import ${packageName}.helper.mybatis.model.${dbTablePrefixU}${dbTableJavaNameU};
import ${packageName}.helper.mybatis.model.${dbTablePrefixU}${dbTableJavaNameU}Example;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.exception.ServiceException;
import com.fosun.spr.core.utils.ExtAsserts;


@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class ${moduleNameU}ServiceImpl extends CoBaseService implements
		${moduleNameU}Service {
	@Autowired 
	private ${moduleNameU}Dao ${moduleName}Dao;
	@Autowired
	private ${dbTablePrefixU}${dbTableJavaNameU}Mapper ${dbTablePrefix}${dbTableJavaNameU}Mapper;

	@Override
	public ListSrModel list(ListSpModel spModel) {
		ListSrModel srModel = new ListSrModel();
		srModel.setDatas(${moduleName}Dao.list(spModel));
		srModel.setTotalCount(${moduleName}Dao.listCount(spModel));
		return srModel;
	}

	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		GetSrModel srModel = new GetSrModel();
		${dbTablePrefixU}${dbTableJavaNameU} db = ${dbTablePrefix}${dbTableJavaNameU}Mapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db,"${moduleLabel}");
		cglibCopyProperties(db, srModel, true);	
		return srModel;
	}
	
	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		DeleteSrModel srModel = new DeleteSrModel();
		${dbTablePrefixU}${dbTableJavaNameU} db = ${dbTablePrefix}${dbTableJavaNameU}Mapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db);
		db.setIsDeleted(IS_DELETED_Y);
		db.setRecUpdateTime(new Date());
		${dbTablePrefix}${dbTableJavaNameU}Mapper.updateByPrimaryKey(db);
		return srModel;
	}
	
	@Override
	public CreateOrUpdateSrModel createOrUpdate(CreateOrUpdateSpModel spModel) {
		<#list tableColumnModelList as item> 
		<#if item.isNotNull=="1">
		ExtAsserts.notNull(spModel.get${item.javaFieldNameU}(),"${item.comment}");
		</#if>
		</#list>
		
		CreateOrUpdateSrModel srModel = new CreateOrUpdateSrModel();
		<#if uniqueColumn?? && uniqueColumn!="">
		//唯一性校验
		${dbTablePrefixU}${dbTableJavaNameU}Example e${uniqueColumnJavaNameU} = new ${dbTablePrefixU}${dbTableJavaNameU}Example();
		${dbTablePrefixU}${dbTableJavaNameU}Example.Criteria c${uniqueColumnJavaNameU} = e${uniqueColumnJavaNameU}.createCriteria();
		c${uniqueColumnJavaNameU}.and${uniqueColumnJavaNameU}EqualTo(spModel.get${uniqueColumnJavaNameU}());
		c${uniqueColumnJavaNameU}.andIsDeletedEqualTo(IS_DELETED_N);
		List<${dbTablePrefixU}${dbTableJavaNameU}> rows${uniqueColumnJavaNameU} = ${dbTablePrefix}${dbTableJavaNameU}Mapper.selectByExample(e${uniqueColumnJavaNameU});		
		</#if>
		
		//if(StringUtils.isBlank(spModel.getId())){
		if(spModel.getId()==null){
			<#if uniqueColumn?? && uniqueColumn!="">
			if(rows${uniqueColumnJavaNameU}.size()>0) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.get${uniqueColumnJavaNameU}()+" 已经存在");
			}		
			</#if>
			
			${dbTablePrefixU}${dbTableJavaNameU} db = new ${dbTablePrefixU}${dbTableJavaNameU}();
			cglibCopyProperties(spModel, db, true);
			<#if insertModelJavaAddtionAtService?? && insertModelJavaAddtionAtService!="">
				${insertModelJavaAddtionAtService}
			</#if>
			db.setIsDeleted(IS_DELETED_N);
			db.setRecCreateTime(new Date());
			//db.setId(getUuid());
			${dbTablePrefix}${dbTableJavaNameU}Mapper.insert(db);
			srModel.setId(db.getId());
		}else{
			<#if uniqueColumn?? && uniqueColumn!="">
			if(rows${uniqueColumnJavaNameU}.size()>1) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.get${uniqueColumnJavaNameU}()+" 已经存在");
			}			
			if(rows${uniqueColumnJavaNameU}.size()==1 && !rows${uniqueColumnJavaNameU}.get(0).get${uniqueColumnJavaNameU}().equals(spModel.get${uniqueColumnJavaNameU}())) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.get${uniqueColumnJavaNameU}()+" 已经存在");
			}	
			</#if>
			
			${dbTablePrefixU}${dbTableJavaNameU} db = ${dbTablePrefix}${dbTableJavaNameU}Mapper.selectByPrimaryKey(spModel.getId());
			<#list tableColumnModelList as item> 
			<#if item.columnType=="db">
			db.set${item.javaFieldNameU}(spModel.get${item.javaFieldNameU}());
			</#if>
			</#list>	
			<#if updateModelJavaAddtionAtService?? && updateModelJavaAddtionAtService!="">
			${updateModelJavaAddtionAtService}
			</#if>
			${dbTablePrefix}${dbTableJavaNameU}Mapper.updateByPrimaryKey(db);
		}
		return srModel;
	}	
}
